//发起新回话
function newConversation() {
    contactObject.getUserInfo(contactId, function (result) {
        userInfo = JSON.parse(result);
        $('#session-no-record').addClass('hideList');
        var sessionId;
        if (userInfo.userId > myInfo.id) {
            sessionId = myInfo.id + '_' + userInfo.userId;
        } else if (userInfo.id < myInfo.id) {
            sessionId = userInfo.userId + '_' + myInfo.id;
        } else {
            sessionId = userInfo.userId + '_' + myInfo.id;
        }

        var session = $('#session-' + sessionId);
        //不存在回话
        if (session.length == 0) {
            var temp = '<li id="session-' + sessionId + '" number="0" session-name="' + userInfo.username + '" sessionId="' + sessionId + '" sessionType="1" userId="' + userInfo.userId + '">' +
                '<span class=""></span><span class="num hideList">0</span><img src="' + userInfo.avatar + '" class="list-name"><div class="list-text">' +
                '<h2><span class="session-name">' + userInfo.username + '</span><span class="time"></span></h2>' +
                '<p><em class="iconn-35 hideList"></em><span class="session-list-at-notice hideList">[有人@我]</span><span class="session-list-announcement-notice hideList">[群公告]</span><span class="red hideList">[草稿]</span>' +
                '<span class="session-text"></span></p><span class=""></span></div></li>';
            $('#session').prepend(temp);
            $('#session-no-record').hide();
        }
        $('#session-' + sessionId).click();

        $('#im-chat').click();
    })
}

/**
 * 点击回话
 */
function openSession(session) {

    console.log(session);

    $(session).siblings().removeClass('active');
    $(session).addClass('active');

    $('#chat-title').html($(session).attr('session-name'));

    var children = $('#msgArea').children().hide();

    var msgArea = $('#msgArea-' + $(session).attr('sessionId'));
    if (msgArea.length == 0) {
        var temp = '<div id="msgArea-' + $(session).attr('sessionId') + '"class="con-list" onmousewheel="loadMore_(this)"><div id="msg"></div></div>';
        $('#msgArea').append(temp);
    }else{
        $(msgArea).show();
        $(msgArea).siblings().hide();
    }
    $('#msg-no-record').hide();
    $('#chatArea').show();

    var sessionId = $(session).attr('sessionId');
    var sessionType = $(session).attr('sessionType');
    var userId = $(session).attr('userId');
    chatObj = {
        'sessionId': sessionId,
        'sessionType': sessionType,
        'userId': userId
    }

}

/**
 * 发送消息
 */
function sendMsg() {
    var msgBody = $('#input-content').html().trim();
    if (msgBody != '') {
        var msgId = Math.uuid();
        var msg = {
            'msgId': msgId,
            'msgBody': msgBody,
            'msgType': 1,
            'sessionId': chatObj['sessionId'],
            'sessionType': parseInt(chatObj['sessionType']),
            'receiverId': parseInt(chatObj['userId'])
        };
        var temp = '<pre id="msg-' + msgId + '" status="1" class="list-right clearfix">' +
            '<img src="' + myInfo.avatar + '" class="list-name">' +
            '<div class="list-text" data-status=""  data-sendtime="' + new Date().getTime() + '"><pre>' + msgBody + '</pre>' +
            '<em class="loading" msgId="' + msgId + '" onclick="resendMsg(this)"><img src="../images/public/loading-ms.gif"></em></div></pre>';
        $('#msgArea-' + chatObj['sessionId'] + ' #msg').append(temp);
        $('#input-content').html('');
        var msgHeight = $('#msgArea-' + chatObj['sessionId'])[0].scrollHeight;
        $('#msgArea-' + chatObj['sessionId']).scrollTop(msgHeight);
        msgObject.sendMsg(JSON.stringify(msg));
    }

}

function receiveMsg(data){
    console.log('收到消息',data);
    var msg = JSON.parse(data);

    //判断会话列表
    var session = $('#session-'+msg.sessionId);
    if(session.length == 0){
        contactObject.getUserInfo(msg.senderId,function (result) {
            userInfo = JSON.parse(result);
            var temp = '<li id="session-' + msg.sessionId + '" number="0" session-name="' + userInfo.username + '" sessionId="' + msg.sessionId + '" sessionType="1" userId="' + msg.senderId + '">' +
                '<span class=""></span><span class="num hideList">1</span><img src="' + userInfo.avatar + '" class="list-name"><div class="list-text">' +
                '<h2><span class="session-name">' + userInfo.username + '</span><span class="time"></span></h2>' +
                '<p><em class="iconn-35 hideList"></em><span class="session-list-at-notice hideList">[有人@我]</span><span class="session-list-announcement-notice hideList">[群公告]</span><span class="red hideList">[草稿]</span>' +
                '<span class="session-text">'+msg.msgBody+'</span></p><span class=""></span></div></li>';
            $('#session').prepend(temp);
        });
    }else{
        $('#session-'+msg.sessionId).find('.session-text').html(msg.msgBody);
    }
    //判断消息区
    var msgArea = $('#msgArea-'+msg.sessionId);
    if(msgArea.length != 0){
        var avatarSrc = $('#session-'+msg.sessionId).find('.list-name').attr('src');
        var temp = '<pre id="msg-' + msg.msgId + '" status="0" class="list-left clearfix">' +
            '<img src="' + avatarSrc + '" class="list-name">' +
            '<div class="list-text" data-status=""  data-sendtime="' + new Date().getTime() + '"><pre>' + msg.msgBody + '</pre>' +
            '</div></pre>';
        $('#msgArea-' + msg.sessionId + ' #msg').append(temp);
    }
    if(msg.sessionId == chatObj['sessionId']){
        var msgHeight = $('#msgArea-' + msg.sessionId)[0].scrollHeight;
        var st = $('#msgArea-' + msg.sessionId).scrollTop();
        var hhhh = $('#msgArea-' + msg.sessionId).height();
        if(msgHeight - st < hhhh){
            $('#msgArea-' + msg.sessionId).scrollTop(msgHeight);
        }
    }
}

function ackMsg(str) {
    ack = JSON.parse(str)
    console.log('ack',ack);
    $('#session-'+ack.sessionId).find('.conn-35').hide();
    $('#msgArea-'+ack.sessionId).find('#msg-'+ack.msgId).find('.loading').hide();
}
